package com.orangelife.mobile.main.fragment;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.curry.android.http.HttpUtil;
import com.curry.android.util.DialogHelper;
import com.curry.orangelife.mobile.R;
import com.markupartist.android.widget.PullToRefreshListView;
import com.orangelife.mobile.common.biz.JSONRequest;
import com.orangelife.mobile.common.fragment.PullRefreshBaseFragment;
import com.orangelife.mobile.common.view.EmptyLayout;
import com.orangelife.mobile.constants.Constant;
import com.orangelife.mobile.main.adapter.TopRecommendAdapter;
import com.orangelife.mobile.surrounding.biz.SurroundHttpBiz;
import com.orangelife.mobile.util.BLog;
import com.orangelife.mobile.util.JsonReponseHandler;
import com.orangelife.mobile.util.ToastHelper;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 发现
 *
 * @author chenxiang
 */
@Deprecated
public class Page2Fragment extends PullRefreshBaseFragment {

    private View homePageView;

    @Bind(R.id.lv_trader)
    PullToRefreshListView pullListView;
    @Bind(R.id.emptyLayout)
    EmptyLayout emptyLayout;
    TopRecommendAdapter<Map<String, Object>> adapter;
    List<Map<String, Object>> data = new ArrayList<>();

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        if (homePageView == null) {
            homePageView = inflater.inflate(R.layout.fragment_find,
                    container, false);
            ButterKnife.bind(this, homePageView);
            setAdapter();
            setXListViewAndEmptyLayoutListener(pullListView,emptyLayout,adapter);
        }
        /*
         * 缓存的viewiew需要判断是否已经被加过parent，
         * 如果有parent需要从parent删除，要不然会发生这个view已经有parent的错误。
         */
        ViewGroup parent = (ViewGroup) homePageView.getParent();
        if (parent != null) {
            parent.removeView(homePageView);
        }
        return homePageView;
    }

    @Override
    protected EmptyLayout getEmptyLayout() {
        return emptyLayout;
    }

    @Override
    protected List getData() {
        return data;
    }

    @Override
    protected int getDataCount() {
        return data.size();
    }

    @Override
    protected void requestWhenPullRefresh() {
        Map<String, Object> map = new HashMap<>();
        map.put("pageIndex", pageNo);
        map.put("pageSize", pageSize);
        Map<String, Object> rootMap = new HashMap<>();
        rootMap.put("map", map);
        rootMap.put("url", Constant.URL_FIND);
        rootMap.put("wat", 1);
        new JSONRequest(JSONRequest.GET, rootMap, handler, HttpUtil.ACTION_URI);
    }

    private void setAdapter() {
        adapter = new TopRecommendAdapter<>(
                getActivity(), data);
    }

    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 1:
                    ArrayList list = (ArrayList) JsonReponseHandler.getListFromJson(String.valueOf(msg.obj), adapter, dialog);
                    int total = SurroundHttpBiz.getGoodListTotal(msg.obj);
                    handleAfterRequest(isConnected(false),total,list,pullListView,adapter);
                    break;
                case 500:
                    if (isAdded()) {
                        ToastHelper.getInstance()._toast(
                                getResources().getString(
                                        R.string.net_new_request));
                    }
                    DialogHelper.closeDialog(dialog);
                    break;
                case 1011:
                    isLogin(dialog);
                    break;
                default:
                    break;
            }
        }
    };

    @Override
    public void onResume() {
        super.onResume();
        BLog.getInstance().intAppmod(BLog.DISCOVER);
    }

    @Override
    public void onStop() {
        super.onStop();
        BLog.getInstance().outAppmod(BLog.DISCOVER);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(getActivity());
    }
}
